package lec15dfs;

import java.util.Scanner;

public class H2 {
    static int n = 8;
    static int[] a = new int[n];
    static boolean[] book = new boolean[n];

    static void dfs(int k) {
        if (k == n) {
            for (int i = 0; i < n; i++)
                System.out.print(a[i]);
            System.out.println();

            return;// 1
        }
        for (int i = 0; i < n; i++) {
            if (book[i])
                continue;
            if (k >= 1) {//说明棋盘上至少有k-1个已经放置且不冲突的皇后，打算把第k个皇后放置在(k,i)位置，需要检验会不会引起新的冲突
                boolean ok = true;
                for (int j = 0; j < k; j++) {
                    int dx = k - j;
                    int dy = i - a[j];
                    if (dx == dy || dx + dy == 0) {
                        ok = false;
                        break;
                    }
                }
                if (!ok) continue;
            }
            a[k] = i;
            book[i] = true;
            dfs(k + 1);
            book[i] = false;
        }
    }

    public static void main(String[] args) {
        dfs(0);
    }

    static Scanner cin = new Scanner(System.in);
}